//app.js
App({
  globalData:{
      appid:'wx06233083b1c51150',
      secret:'34262f185ef04f89593d8b1e9019e26a',
      openid:'',
      id:'',
      name:'',
      major:'',
      grade:'',
      phone:''
  },
  onLaunch: function () {
    var that = this;
    var user=wx.getStorageSync('user') || {};
        wx.login({  
        success: function(res){ 
            if(res.code) {
                wx.getUserInfo({
                    success: function (res) {
                        var objz={};
                        objz.avatarUrl=res.userInfo.avatarUrl;
                        objz.nickName=res.userInfo.nickName;
                        //console.log(objz);
                        wx.setStorageSync('userInfo', objz);//存储userInfo
                    }
                });
                var d=that.globalData;//这里存储了appid、secret、token串  
                var l='https://api.weixin.qq.com/sns/jscode2session?appid='+d.appid+'&secret='+d.secret+'&js_code='+res.code+'&grant_type=authorization_code';  
                wx.request({  
                    url: l,  
                    data: {},  
                    method: 'GET', 
                    success: function(res){ 
                        var obj={};
                        obj.openid=res.data.openid;  
                        obj.expires_in=Date.now()+res.data.expires_in;  
                        wx.setStorageSync('user', obj);//存储openid  
                        getApp().globalData.openid = user.openid;
                        getApp().getUser();
                       
                    }  
                });
            }else {
                console.log('获取用户登录态失败！' + res.errMsg)
            }          
        }  
    });
    
 },
 getUser: function(){
    var MyOpenid = getApp().globalData.openid;
    wx.request({
        url: "http://localhost:8080/clock_war_exploded/user/findByOpenid",
        method:"GET",
        data:{
            openid:MyOpenid
        },
        success: function (res) {
            if (res.data){
                getApp().globalData.id = res.data.id;
                getApp().globalData.name = res.data.name;
                getApp().globalData.major = res.data.major;
                getApp().globalData.grade = res.data.grade;
                getApp().globalData.phone = res.data.phone;
             wx.showToast({
              title: '已获取登录信息',
              icon:'success',
             })
            }else{
                wx.showToast({
                    title: '未检测到个人信息，请完善个人信息',
                    icon:'none',
                })
                setTimeout(function() {
                    wx.navigateTo({
                        url: '/pages/student/myDetails/myDetails',
                      })
                },2000)
                
            }        
        }
    })
 },
})
